import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('cart', () => {
  let cart=ref([])
   let id=ref (0)
   function addcart(cartData,goodsid)
   {
    let username=localStorage.getItem('username')
    let usercart=cart.value.filter(item=>item.username==username)
    let pdcart=usercart.find(item=>item.goods.goods_id==goodsid)
    if(pdcart)
    {
    pdcart.value++
    }else
    {
     cart.value.push({
      username:username,
      id:id.value++,
      number:1,
      goods:cartData,
      checked:false
     })
    }
   }
   function delCart(id)
   {
      cart.value=cart.value.filter(item=>item.id==id)
   }
  return {cart,addcart,delCart }
},
 {
    persist: true,
  }
)
